﻿using System;
using System.Collections.Generic;

using System.Security.Principal;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Web.SessionState;

namespace DotNETX.Web.Handlers
{

    public abstract class BaseHandler : IHttpHandler
    {
        private HttpContext _context;

        #region IHttpHandler

        public virtual bool IsReusable { get { return false; } }

        public abstract void ProcessRequest(HttpContext context);

        #endregion

        /// <summary>
        /// only disable client cache
        /// </summary>
        public void SetNoCache()
        {
            Response.Buffer = true;
            Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
            Response.Expires = 0;
            Response.CacheControl = "no-cache"; 
        }

        protected HttpContext Context
        {
            get
            {
                if (_context == null)
                    _context = HttpContext.Current;

                return _context;
            }
            set { _context = value; }
        }

        protected HttpApplicationState Application
        {
            get
            {
                if (Context != null)
                    return Context.Application;

                return null;
            }
        }

        protected HttpApplication ApplicationInstance
        {
            get
            {
                if (Context != null)
                    return Context.ApplicationInstance;

                return null;
            }
        }

        protected Cache Cache
        {
            get
            {
                if (Context != null)
                    return Context.Cache;

                return null;
            }
        }

        protected HttpRequest Request
        {
            get
            {
                if (Context != null)
                    return Context.Request;

                return null;
            }
        }

        protected HttpResponse Response
        {
            get
            {
                if (Context != null)
                    return Context.Response;

                return null;
            }
        }

        protected HttpSessionState Session
        {
            get
            {
                if (Context != null)
                    return Context.Session;

                return null;
            }
        }

        protected HttpServerUtility Server
        {
            get
            {
                if (Context != null)
                    return Context.Server;

                return null;
            }
        }

        protected TraceContext Trace
        {
            get
            {
                if (Context != null)
                    return Context.Trace;

                return null;
            }
        }

        public IPrincipal User
        {
            get
            {
                if (Context != null)
                    return Context.User;

                return null;
            }
        }
    }
}
